package com.gxa.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.uitls.R;
import com.gxa.dto.RepositoryDto;
import com.gxa.dto.StockUpdateDto;
import com.gxa.entity.Cargo;
import com.gxa.entity.Emp;
import com.gxa.entity.Repository;
import com.gxa.service.RepositoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "仓库接口")
@RestController
public class RepositoryController {

    @Autowired
   private RepositoryService repositoryService ;
    @GetMapping("/repository/structure")
    @ApiOperation("点击仓库结构，呈现数据")
    public R queryStructure(Integer page,Integer limit) {
        try {
            Page<Map<String, Object>> pageHelper = PageHelper.startPage(page, limit);
            List<Repository> repositories = repositoryService.queryStructure();
            Map<String, Object> map = new HashMap();
            map.put("repositories", repositories);
            map.put("count", pageHelper.getTotal());
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询数据失败");

        }
    }

    @PostMapping("/repository/repositoryAdd")
    @ApiOperation("添加仓库确定")
    public R addNew(@RequestBody Repository repository){

        try {
            repositoryService.addNew(repository);
            return R.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败");
        }
    }

    @PostMapping("/repository/areaAdd")
    @ApiOperation("添加仓库结构区域数据")
    public R addArea(@RequestBody Cargo cargo){

        try {
            repositoryService.addArea(cargo);
            return R.ok("添加成功");
        }catch (Exception e) {
            e.printStackTrace();
            return R.error(1, "添加失败");
        }
    }

    @ApiOperation("点击新增货架，根据当前仓库名称给所属仓库赋值，并且返回仓库名称，接收查询到的区域名称集合")
    @PostMapping("/repository/queryArea")
    public R queryArea(@ApiParam("仓库名") String rname){

        try {
            List<Cargo> cargos = repositoryService.queryLevelByRnameCargos(rname);
            Map<String,Object> map = new HashMap();
            map.put("cargos",cargos);
            return R.ok(map);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("查询失败");
        }
    }

    @PostMapping("/repository/cargoAdd")
    @ApiOperation("点击添加仓库结构货架数据，返回当前仓库名称，货架名称，区域名称")
    public R addCargo(@RequestBody Cargo cargo){
        try {
            repositoryService.addCargo(cargo);
            return R.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败");
        }
    }



    @PostMapping("/repository/warehouse")
    @ApiOperation("点击仓储管理，呈现数据,且按条件查询")
    public R  queryWarehouse(@RequestBody RepositoryDto repositoryDto){
      try {
          Page<Map<String,Object>> pageHelper = PageHelper.startPage(repositoryDto.getPage(), repositoryDto.getLimit());
          List<Repository> repositories = this.repositoryService.queryAll(repositoryDto);
          Map<String,Object> map = new HashMap<>();
          map.put("repositories",repositories);
          map.put("count",pageHelper.getTotal());
          return R.ok(map);

     }  catch (Exception e) {
          e.printStackTrace();
          return R.error("查询失败");
      }
    }

    @GetMapping("/repository/queryHouseAndArea")
    @ApiOperation("点击筛选，查询已有仓库和区域")
    public R queryStorehouse(){
        try {
            List<Repository> houseAndArea = this.repositoryService.queryStoreAndArea();
            Map<String,Object> map = new HashMap();
            map.put("houseAndArea",houseAndArea);
            return R.ok(map);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("查询失败");
        }
    }


//    @GetMapping("/repository/queryLocation")
//    @ApiOperation("点击调拨，查询货位")
//    public R queryLocation(){
//        try {
//            List<String> cargos = repositoryService.queryLocation();
//            Map<String,Object> location = new HashMap();
//            location.put("location",location);
//            return R.ok(location);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return R.error("查询失败");
//        }
//
//    }



    @PostMapping("/repository/Allocate")
    @ApiOperation("调拨确定")
    public R updateAllocate (@RequestBody  StockUpdateDto stockUpdateDto) {
        try {
            repositoryService.updateStock(stockUpdateDto);
            return R.ok("调拨成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("调拨失败");
        }
    }

    @GetMapping("/repository/queryEmp")
    @ApiOperation("查询雇员")
    public R updateAllocate () {
        try {
            List<Emp> emps = repositoryService.queryEmp();
            Map<String,Object> map = new HashMap<>();
            map.put("emps",emps);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("无");
        }
    }





    }


